import * as fastFilterService from '../services/services';

export default {
  namespace: 'fastFilter',
  state: {
    a: [],
  },
  reducers: {
  },
  effects: {
    *getFastData({ payload }, { put, call, select }) {
      /* 获取快捷过滤 */
      const { isRefresh } = payload;
      if (isRefresh) {
        const { componentName, sDataListTabId } = payload;
        const tableConfig = {
          sDataListTabId, componentName: `fast${componentName}`, firstColumns: [], isRefresh, configType: 'fast',
        };
        yield put({ type: 'mergeTable/setTableConfig', payload: { tableConfig } });
      } else {
        const {
          componentName, fastUrl, firstColumns, sId, sParentId,
        } = payload;
        const token = yield select(state => state.app.token);
        const sDataListTabId = yield select(state => state.app.sTabId);
        const { data } = yield call(fastFilterService.getService, token, fastUrl);
        if (data.code === 1) {
          const { rows: fData } = data.dataset; /* 整合前快捷过滤数据 */
          const tableConfig = {
            sDataListTabId, componentName: `fast${componentName}`, fData, fastUrl, firstColumns, sId, sParentId, configType: 'fast',
          };
          yield put({ type: 'mergeTable/setTableConfig', payload: { tableConfig } });
        } else {
          yield put({ type: 'app/throwError', payload: data });
        }
      }
    },
    *addFastData({ payload }, { put, call, select }) {
      /* 新增快捷过滤模板 */
      const token = yield select(state => state.app.token);
      const {
        value, addUrl, sTableId, componentName, fastFlag,
      } = payload;
      const { data } = yield call(fastFilterService.postValueService, token, value, addUrl);
      if (data.code === 1) {
        const fData = data.dataset.rows; /* 添加方案 */
        const dataList = {
          sDataListTabId: sTableId, componentName, urlData: fData, fastFlag,
        };
        yield put({ type: 'content/setDataList', payload: { dataList } });
        // yield put({ type: 'content/getUrlData', payload: { sTabId: sTableId, componentName, url: getFastUrl } });
        // const tableConfig = { sDataListTabId, componentName, fData, fastUrl, firstColumns, sId, sParentId, configType: 'fast' };
        // yield put({ type: 'mergeTable/setTableConfig', payload: { tableConfig } });
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
    *delFastData({ payload }, { put, call, select }) {
      /* 删除快捷过滤模板 */
      const token = yield select(state => state.app.token);
      const { delUrl, fConfig, fastFlag } = payload;
      const { componentName, sDataListTabId/* , firstColumns, sId, sParentId */ } = fConfig;
      const { data } = yield call(fastFilterService.getService, token, delUrl);
      if (data.code === 1) {
        const fData = data.dataset.rows; /* 删除方案 */
        const dataList = {
          sDataListTabId, componentName, urlData: fData, loading: false, fastFlag,
        };
        yield put({ type: 'content/setDataList', payload: { dataList } });
        /*  const tableConfig = { sDataListTabId, componentName, fData, fastUrl, firstColumns, sId, sParentId, configType: 'fast' };
          yield put({ type: 'mergeTable/setTableConfig', payload: { tableConfig } }); */
      } else {
        yield put({ type: 'app/throwError', payload: data });
      }
    },
  },
  subscriptions: {},
};
